﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace InSys
{
    public partial class FormLogin : Form
    {
        private int idDepartment = 0;
        private int idUser = 0;
        public FormLogin()
        {
            InitializeComponent();
            userControlSelectUser1.IdUser = Properties.Settings.Default.DefaultUser;
            userControlSelectUser1.IdDepartment = Properties.Settings.Default.DefaultDepartment;
        }

        public int IdDepartment { get { return idDepartment; } }
        public int IdUser { get { return idUser; } }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            CancelLogin();
        }

        private void buttonAccept_Click(object sender, EventArgs e)
        {
            ValidateInput();
        }

        private void CancelLogin()
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void userControlSelectUser1_UserControlSelectUserEvent(object sender, EventArgs e)
        {
            ValidateInput();
        }

        private void ValidateInput()
        {
            if (true == userControlSelectUser1.IsEmptyUserList)
            {
                idUser = 0;
                idDepartment = 0;
                DialogResult = System.Windows.Forms.DialogResult.OK;
                return;
            }
            if (0 == userControlSelectUser1.IdUser | 0 == userControlSelectUser1.IdDepartment)
            {
                DialogResult = System.Windows.Forms.DialogResult.Abort;
                return;
            }
            idUser = userControlSelectUser1.IdUser;
            Properties.Settings.Default.DefaultUser = idUser;
            idDepartment = userControlSelectUser1.IdDepartment;
            Properties.Settings.Default.DefaultDepartment = idDepartment;
            Properties.Settings.Default.Save();
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
